(function(app){
	var download = "/homePage/myInsured/insured.json?appId=6103_web&sig=xxx"
	// 好友列表
	app.action(['/insured/frindList'], 'insured2/page.html', ['$scope', '$route', '_API', '$location', 	'$timeout', '$rootScope',
		function($scope, $route, _API, $location, $timeout, $rootScope) {
			function reload(){
				_API.ajax('person/contact/list',{})
				.success(function(json){
					$scope.data = json.result;
				})
				.error(function(json){
					if(json.code == '201'){
						alert("请登陆手机端，完成实名认证！");
						$location.path("/certification");
					}
				});
			}
			reload();

			$scope.save = function() {
				var $subBox = $("input[name='radioBox']");
				var ids = "";
				for (var i=0; i<$subBox.length; i++) {
					var box = $subBox[i];
					if(box.checked){
						ids = box.value;
					}
				}
				if(ids == "") {
					alert("请选择要参保的用户。");
					return;
				}

				$location.path("/insured/next/"+ids);
			}
		}
	]);
	// 参保编辑页面
	app.action(['/insured/next/:ids'], 'insured2/edit.html', 
			['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
   		function($scope, $route, _API, $location, $timeout, $rootScope) {
			// 页面初始化数据
			var CA001 = $route.current.params.ids;
			if(CA001 != 1){
				_API.ajax('homePage/myTransfer/getInfoByCa001',{"CA001":CA001})
				.success(function(json){
					$scope.data3 = json.result;
				});
			}
			
			// 初始化行政区划
			_API.ajax('dict/organization',{})
			.success(function(json){
				$scope.data = json.result;
				$scope.data.AAC009="20";
				$scope.data.AAC012="2";
				// $scope.data.AAZ065 = "JH";
			});
			// 选择行政区划
			$scope.organClick = function(aab301,aac009) {
				if(aab301 != "" && aab301 != undefined) {
					_API.ajax('dict/streetTown',{"AAB301":aab301})
					.success(function(json){
						$scope.data1 = json.result;
					});
					if(aac009 != "" && aac009 != undefined) {
						// 缴费档次
						_API.ajax('dict/paygrade',{"AAA027":aab301,"AAA118":aac009})
						.success(function(json){
							$scope.data4 = json.result;
						});
					} else {
						$scope.data4 = null;
					}
				} else {
					$scope.data1 = null;
					$scope.data2 = null;
					$scope.data4 = null;
				}
			}
			// 选择镇
			$scope.organVillageClick = function(aab301) {
				if(aab301 != "" && aab301 != undefined) {
					_API.ajax('dict/village',{"AAB301":aab301})
					.success(function(json){
						$scope.data2 = json.result;
					});
				} else {
					$scope.data2 = null;
				}
			}
			// 户口性质
			$scope.aac009Click = function(aab301,aac009) {
				if(aab301 != "" && aab301 != undefined && aac009 != "" && aac009 != undefined) {
					// 缴费档次
					_API.ajax('dict/paygrade',{"AAA027":aab301,"AAA118":aac009})
					.success(function(json){
						$scope.data4 = json.result;
					});
				} else {
					$scope.data4 = null;
				}
			}
			// 参保-图片上传，保存操作
			$scope.submit = function(data,data3) {
				if(data3 == null) {
					alert("请选择要参保的用户。");
					return;
				}

				if(!$("#form").valid()) {
					return false;
				}

				var fileObj = document.getElementById("exampleInputFile").files[0]; // 身份证照片（正面）
				var fileObj1 = document.getElementById("exampleInputFile1").files[0]; // 身份证照片（反面）
				var fileObj2 = document.getElementById("exampleInputFile2").files[0]; // 户口本图片
				// var fileSize = fileObj.size;
				// var size = fileSize / 1024;    
				// if(size>500){  
				//    alert("附件不能大于500k");
				//    return;
				// }
				// var fileSize1 = fileObj1.size;
				// var size1 = fileSize1 / 1024;    
				// if(size>500){  
				//    alert("附件不能大于500k");
				//    return;
				// }
				// var fileSize2 = fileObj2.size;
				// var size2 = fileSize2 / 1024;    
				// if(size>500){  
				//    alert("附件不能大于500k");
				//    return;
				// }
				var form = new FormData();
				form.append("BIC106", fileObj); // 身份证照片（正面）
				form.append("BIC104", fileObj1); // 身份证照片（反面）
				form.append("BIC105", fileObj2); // 户口本图片
				form.append("CA001", data3.CA001);
				form.append("AAB301", data.intoAddress);
				form.append("AAZ070", data.intoStreet);
				form.append("AAC009", data.AAC009);
				form.append("AAC012", data.AAC012);
				form.append("AAE004", data.AAE004);
				form.append("AAE005", data.AAE005);
				form.append("AAE006", data.AAE006);
				form.append("AAE159", data.AAE159);
				form.append("AAZ289", data.AAZ289);
				$.ajax({
					"url": $SDC.apiDomain+download,
					"dataType": "JSON",
					"data": form,
					"type": "POST",
					"cache": false,
					"timeout": 30000,
					"xhrFields": {
						"withCredentials": true
					},
					"crossDomain": true,
					processData: false,
					contentType: false,
					success: function(json) {
						console.log(json);
						if(json.result.isPass) {
							alert("您的参保信息已提交，请等待工作人员复核！");
							location.hash ="/hello";
				        } else {
				        	alert(json.result.info);
				        }
				        
					}
				});
			}
   		}
   	]);
})($SDC.app);
